package cn.samir.sudoku.util;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import cn.samir.sudoku.R;
import cn.samir.sudoku.adapter.ShareGridViewAdapter;
import cn.samir.sudoku.keys.Conf;
import cn.samir.sudoku.share.ShareItem;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;

public class SharePopWindow implements OnItemClickListener, OnClickListener, PlatformActionListener {
	public static final int QQhaoyou = 1;

	public static final int Qzone = 2;
	public static final int Wxzone = 3;
	public static final int Weixinhaoyou = 4;
	public static final int SinaWeibo = 5;
	public static final int TencentWeibo = 6;

	private static final String TAG = "SharePopWindow";
	Activity context;
	PopupWindow popupWindow;
	ArrayList<ShareItem> items;
	View contentView;
	GridView gridView;
	TextView tv_cancel;
	ShareGridViewAdapter shareGridViewAdapter;
	ShareParams mShareParams = null;

	public SharePopWindow(Activity context) {
		this.context = context;
		initViews();
	}

	private void initViews() {
		contentView = View.inflate(context, R.layout.popwindow_share, null);
		tv_cancel = (TextView) contentView.findViewById(R.id.tv_cancel);
		tv_cancel.setOnClickListener(this);
		gridView = (GridView) contentView.findViewById(R.id.gridview_popwindow);
		initShareItem();
		shareGridViewAdapter = new ShareGridViewAdapter(context);
		gridView.setNumColumns(3);
		shareGridViewAdapter.appendToList(items);
		gridView.setOnItemClickListener(this);
		gridView.setAdapter(shareGridViewAdapter);
		popupWindow = new PopupWindow(context);
		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
		popupWindow.setWidth(LayoutParams.MATCH_PARENT);
		popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setContentView(contentView);

	}

	private void initShareItem() {
		items = new ArrayList<ShareItem>(6);
		items.add(0, new ShareItem("QQ好友", QQhaoyou, R.drawable.logo_qq));
		items.add(1, new ShareItem("QQ空间", Qzone, R.drawable.logo_qzone));
		items.add(2, new ShareItem("微信朋友圈", Wxzone, R.drawable.logo_wechatmoments));
		items.add(3, new ShareItem("微信好友", Weixinhaoyou, R.drawable.logo_wechat));
		items.add(4, new ShareItem("新浪微博", SinaWeibo, R.drawable.logo_sinaweibo));
		//items.add(5, new ShareItem("腾讯微博", TencentWeibo, R.drawable.logo_tencentweibo));
	}

	public void show() {
		popupWindow.showAtLocation(context.getWindow().getDecorView(),
				Gravity.BOTTOM, 0, 0);
	}

	public void dismiss() {
		popupWindow.dismiss();
	}

	public ShareParams getShareParams() {
		return mShareParams;
	}

	public void setShareParams(ShareParams shareParams) {
		this.mShareParams = shareParams;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		dismiss();
		if (mShareParams == null) {
			mShareParams = new ShareParams();
		}
		ShareSDK.initSDK(context);
		Platform platform = null;
		ShareItem item = (ShareItem) parent.getItemAtPosition(position);
		switch (item.id) {
		case QQhaoyou:
			/**
			 * QQ空间分享时一定要携带title、titleUrl、site、siteUrl，QQ空间本身不支持分享本地图片，
			 * 因此如果想分享本地图片，图片会先上传到ShareSDK的文件服务器，得到连接以后才分享此链接。
			 * 由于本地图片更耗流量，因此imageUrl优先级高于imagePath。
			 * ShareParams sp = new ShareParams();
			sp.setTitle("测试分享的标题");
			sp.setTitleUrl("http://sharesdk.cn"); // 标题的超链接
			sp.setText("测试分享的文本");
			sp.setImageUrl("http://www.someserver.com/测试图片网络地址.jpg");
			sp.setSite("发布分享的网站名称");
			sp.setSiteUrl("发布分享网站的地址");
			 
			Platform qzone = ShareSDK.getPlatform (QZone.NAME);
			qzone. setPlatformActionListener (paListener); // 设置分享事件回调
			// 执行图文分享
			qzone.share(sp);
			 */
//			mShareParams = new ShareParams();
//			mShareParams.setTitle("Q分享");
//			mShareParams.setTitleUrl("");
//			mShareParams.setImageUrl("http://sudokus.sinaapp.com/logo_96.png");
//			platform = ShareSDK.getPlatform(QQ.NAME);
			ShareParams sp = new ShareParams();
			sp.setTitle("测试分享的标题");
			sp.setTitleUrl(Conf.apkUrl); // 标题的超链接
			sp.setText("测试分享的文本");
			sp.setImageUrl("http://sudokus.sinaapp.com/logo_96.png");
			//sp.setSite("发布分享的网站名称");
			//sp.setSiteUrl("发布分享网站的地址");
			 
			Platform qzone = ShareSDK.getPlatform (QZone.NAME);
			qzone. setPlatformActionListener (this); // 设置分享事件回调
			// 执行图文分享
			qzone.share(sp);
			break;

		case Qzone:
			platform = ShareSDK.getPlatform(QZone.NAME);
			break;

		case Wxzone:
			platform = ShareSDK.getPlatform(Wechat.NAME);
			break;

		case Weixinhaoyou:
			platform = ShareSDK.getPlatform("WechatMoments");
		//	platform = ShareSDK.getPlatform(We);
			break;

		case SinaWeibo:
			platform = ShareSDK.getPlatform(cn.sharesdk.sina.weibo.SinaWeibo.NAME);
			break;

		case TencentWeibo:
			platform = ShareSDK.getPlatform("TencentWeibo");
			break;

		default:
			break;
		}
//		platform.setPlatformActionListener(this);
//		platform.share(mShareParams);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_cancel:
			dismiss();
			break;

		default:
			break;
		}

	}

	@Override
	public void onCancel(Platform arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
		LogUtils.i(TAG, "onComplete");
	}

	@Override
	public void onError(Platform arg0, int arg1, Throwable arg2) {
		// TODO Auto-generated method stub
		
	}

}
